cmake_minimum_required(VERSION 3.10)

# 指定交叉编译工具链的根路径
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

# 设置arm-none-eabi编译工具链
set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)

# 指定编译器
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)

# 指定目标平台的链接器
set(CMAKE_LINKER arm-none-eabi-ld)

# 其他可能需要的工具，如 ar, as, objcopy 等
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_AR arm-none-eabi-ar)

set(CROSS_COMPILE arm-none-eabi-)

project(DTU C ASM)

set(CMAKE_CXX_STANDARD 11)

# 编译二进制文件
add_executable(${PROJECT_NAME} 
    Source/main.c
    Source/tremo_it.c
    Source/lora/apps/App_Mac.c
    Source/lora/apps/App_Lora_Task.c
    Source/lora/apps/App_485_Task.c
    Source/lora/apps/App_Send_Data_Task.c
    Source/lora/apps/App_Service_Task.c
    Source/lora/apps/App_At_Command.c
    Source/lora/apps/App_Modbus_Proto.c
    Source/lora/apps/App_LoRa_Proto.c
    Source/lora/mac/LoRaMac.c
    Source/lora/mac/LoRaMacClassB.c
    Source/lora/mac/LoRaMacConfirmQueue.c
    Source/lora/mac/LoRaMacCrypto.c
    Source/lora/mac/region/Region.c
    Source/lora/mac/region/RegionCommon.c
    Source/lora/mac/region/RegionCN470.c
    Source/lora/driver/board.c
    Source/lora/system/system.c
    Source/lora/driver/utilities.c
    Source/lora/system/timer.c
    Source/lora/system/delay.c
    Source/lora/system/fifo.c
    Source/lora/driver/usart-board.c
    Source/lora/driver/flash-board.c
    Source/lora/driver/rtc-board.c
    Source/lora/driver/sx1262-board.c
    Source/lora/radio/sx126x/sx126x.c
    Source/lora/system/crypto/cmac.c
    Source/lora/radio/sx126x/radio.c
    Source/lora/driver/i2c-board.c
    Source/lora/driver/ads1115-board.c
    Library/platform/system/system_cm4.c
    Library/peripheral/src/tremo_adc.c
    Library/peripheral/src/tremo_bstimer.c
    Library/peripheral/src/tremo_crc.c
    Library/peripheral/src/tremo_dac.c
    Library/peripheral/src/tremo_delay.c
    Library/peripheral/src/tremo_dma.c
    Library/peripheral/src/tremo_flash.c
    Library/peripheral/src/tremo_gpio.c
    Library/peripheral/src/tremo_i2c.c
    Library/peripheral/src/tremo_i2s.c
    Library/peripheral/src/tremo_iwdg.c
    Library/peripheral/src/tremo_lcd.c
    Library/peripheral/src/tremo_lptimer.c
    Library/peripheral/src/tremo_lpuart.c
    Library/peripheral/src/tremo_pwr.c
    Library/peripheral/src/tremo_rcc.c
    Library/peripheral/src/tremo_rtc.c
    Library/peripheral/src/tremo_spi.c
    Library/peripheral/src/tremo_system.c
    Library/peripheral/src/tremo_timer.c
    Library/peripheral/src/tremo_uart.c
    Library/peripheral/src/tremo_wdg.c
    Middleware/FreeRTOS/croutine.c
    Middleware/FreeRTOS/event_groups.c
    Middleware/FreeRTOS/list.c
    Middleware/FreeRTOS/queue.c
    Middleware/FreeRTOS/tasks.c
    Middleware/FreeRTOS/timers.c
    Middleware/FreeRTOS/portable/MemMang/heap_4.c
    Middleware/FreeRTOS/portable/GCC/ARM_CM4F/port.c
    Library/platform/system/printf-stdarg.c
    Library/platform/system/startup_cm4.S
)

# 设置目标架构所需的编译器标志
add_compile_options(-mcpu=cortex-m4 -mfloat-abi=softfp -mfpu=fpv4-sp-d16)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -fsingle-precision-constant -std=gnu99 -ffunction-sections -fno-builtin-printf -fno-builtin-sprintf -fno-builtin-snprintf -fno-common -fmessage-length=0")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

set_source_files_properties(startup_cm4.S PROPERTIES LANGUAGE "ASM")
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_ASM_FLAGS "-x assembler-with-cpp -mcpu=cortex-m4 -mthumb --specs=nosys.specs -c" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T${CMAKE_CURRENT_SOURCE_DIR}/MDK_Project/cfg/gcc.ld -mcpu=cortex-m4 -mthumb -mthumb-interwork -Wl,--gc-sections -Wl,--wrap=printf -Wl,--wrap=sprintf -Wl,--wrap=snprintf")
set(CMAKE_VERBOSE_MAKEFILE ON)

# 添加头文件路径
target_include_directories(${PROJECT_NAME} 
    PRIVATE 
    Library
    Library/crypto/inc
    Library/peripheral/inc
    Library/platform/CMSIS
    Library/platform/system
    Middleware/FreeRTOS/include
    Middleware/FreeRTOS/portable/MemMang
    Middleware/FreeRTOS/portable/GCC/ARM_CM4F
    Source
    Source/lora/apps
    Source/lora/driver
    Source/lora/system/crypto
    Source/lora/radio
    Source/lora/linkwan/inc
    Source/lora/radio/sx126x
    Source/lora/mac
    Source/lora/mac/region
    Source/lora/system
)

# 添加库文件路径
target_link_directories(${PROJECT_NAME} PRIVATE Library/crypto/lib)
# 链接库文件
target_link_libraries(${PROJECT_NAME} PRIVATE crypto gcc m)

#添加宏定义
target_compile_definitions(${PROJECT_NAME} PRIVATE -DCONFIG_DEBUG_UART=UART0 -DREGION_CN470 -DCONFIG_LOG)

# 添加编译选项
target_compile_options(${PROJECT_NAME} PRIVATE -Os)

message("========>"${CMAKE_BINARY_DIR})

add_custom_target (${CMAKE_PROJECT_NAME}.bin 
    ALL 
    arm-none-eabi-objcopy 
    -Obinary 
    "${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}"
    "${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.bin"
    DEPENDS ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME})